<template>
    <base-breadcrumb :title="$route.meta.title">
      <!-- BIM获奖台账编辑 -->
      <a-form-model
        id="base-breadcrumb"
        ref="ruleForm"
        :model="form"
        :label-col="{ span: 6 }"
        :wrapper-col="{ span: 18 }"
        :rules="editModel != 0 ? rules : {}"
        style="position: relative"
      >
        <a-spin :spinning="loading">
          <!-- 基本信息 -->
          <a-card :bordered="false" style="margin-bottom: 50px">
            <title-name title="基本信息" />
            <custom-form
              :is-emit="false"
              :form-list="formList"
              :form="form"
              :edit-model="editModel == 0 ? 'V' : ''"
              :form-components="$refs"
            />
            <title-name title="附件资料" />
            <UploadTableNew
              class="mb20"
              :disabled="editModel == 0"
              :init-file-list="form.fileInfoList"
              business-code="BIM05"
            />
            <!-- 页脚操作栏 -->
            <div class="btn_box page-btn-right-top">
              <a-button
                v-if="editModel != 0"
                :loading="loading"
                type="primary"
                class="button_right"
                @click="handleSubmit"
              >
                提交
              </a-button>
              <a-button v-if="checkVisibleInDetail" type="primary" @click="handleActiveClose"> 关闭 </a-button>
            </div>
          </a-card>
        </a-spin>
      </a-form-model>
    </base-breadcrumb>
  </template>
  
  <script>
  import { insertBimResult, updateBimResult, getBimResultById } from '@/api/bimIntegrate/bimResult.js'
  import { getPageTypesLabel } from '@/api/bimIntegrate/bimResource.js'
  import BaseEditPage from '@/views/baseEditPage' // 编辑详情页基础配置
  import SelectCompanyAndProject from '@/components/Select/SelectCompanyAndProject'
  import Selector from '@/components/Select/Selector'
  
  export default {
    name: 'a' + Date.now(),
    extends: new BaseEditPage(),
    data() {
      return {
        loading: false, // 草稿和提交按钮loding
        editModel: 0, // 操作状态:0 查看;1 编辑;2 新增
        id: '', // 业务id
        form: {
          unitId: '', // 单位id
          unitName: '', // 单位
          fileInfoList: [] // 文件列表
        }, // 表单
        rules: {
          resultName: [{ required: true, whitespace: true, message: '请输入成果名称', trigger: 'change' }],
          finishDate: [{ required: true, message: '请选择完成日期', trigger: 'change' }],
          type: [{ required: true, message: '请选择类别', trigger: 'change' }],
          resultProducer: [{ required: true, whitespace: true, message: '请输入成果制作者', trigger: 'change' }],
          unitId: [{ required: true, message: '请选择成果所有者', trigger: 'change' }],
          resultLevel: [{ required: true, message: '请选择成果等级', trigger: 'change' }]
        },
        // 比赛名称
        CompetitionName:[
                {
                    label:'AEC 全球工程建设业卓越BIM大赛',
                    value:'AEC 全球工程建设业卓越BIM大赛'
                },
                {
                    label:'中国建设工程BIM大赛',
                    value:'中国建设工程BIM大赛'
                },
                {
                    label:'2020 年第十一届 "创新杯"',
                    value:'2020 年第十一届 "创新杯"'
                },
                {
                    label:'建筑信息模型 (BIM) 应用大赛',
                    value:'建筑信息模型 (BIM) 应用大赛'
                },
                {
                    label:'首届工程建设行业BIM大赛',
                    value:'首届工程建设行业BIM大赛'
                },
                {
                    label:'2020年安装行业BIM技术应用大赛',
                    value:'2020年安装行业BIM技术应用大赛'
                },
                 {
                    label:'第九届“龙图杯”全国BIM大赛',
                    value:'第九届“龙图杯”全国BIM大赛'
                },
                {
                    label:'北京市工程建设BIM应用成果交流活动',
                    value:'北京市工程建设BIM应用成果交流活动'
                },
                {
                    label:'湖北省BIM大赛',
                    value:'湖北省BIM大赛'
                },
                {
                    label:'四川省建设工程BIM应用大赛',
                    value:'四川省建设工程BIM应用大赛'
                },
                {
                    label:'中建八局第六届BIM应用大赛',
                    value:'中建八局第六届BIM应用大赛'
                }
            ]
      }
    },
    mounted() {
      this.$route.meta.title = ['成果查看', '成果编辑', '成果新增'][this.editModel]
      // 不为添加时请求详情
      if (this.editModel != 2) {
        this.loading = true
        getBimResultById({
          id: this.id
        })
          .then((res) => {
            this.loading = false
            !res.data.fileInfoList && (res.data.fileInfoList = [])
            this.form = res.data
          })
          .catch(() => {
            this.loading = false
          })
      } else {
        this.form.unitName = this.$currentPost()?.name // 默认单位
        this.form.unitId = this.$currentPost()?.id // 默认单位id
        this.form.unitCode = this.$currentPost()?.code // 默认单位id
      }
    },
    computed: {
      // 表单列表
      formList() {
        return [
        {
          keyName: 'unitId',
          viewName: 'unitName',
          type: SelectCompanyAndProject,
          label: '所属单位',
          attrs: {
            extraParams: {
              notContainTags: ['PD']
            }
          },
          onSelectChange: (e) => {
            this.form.unitName = e.name
          }
        },
          {
            keyName: 'resultProducer',
            type: 'SelectPerson',
            isEcho: true,
            label: '主要完成人',
            personKeys: ['resultProducerId', 'resultProducer'],
            span: 24
          },
          {
            keyName: 'finishDate',
            type: 'a-date-picker',
            label: '获奖时间',
            attrs: {
              valueFormat: 'YYYY-MM-DD'
            }
          },
          {
            keyName: 'type',
            viewName: 'typeName',
            type: 'a-select',
            label: '比赛名称',
            data:this.CompetitionName
            // attrs: {
            //   customApi: getPageTypesLabel,
            //   customQuery: { param: { type: '31' } },
            //   dataValue: 'data.list'
            // }
          },
          {
            keyName: 'awardName',
            type: 'a-select',
            label: '奖项名称',
            data:[
                {
                    label:'一等奖',
                    value:'一等奖'
                },
                {
                    label:'二等奖',
                    value:'二等奖'
                },
                {
                    label:'三等奖',
                    value:'三等奖'
                }
            ]
          },
        //   {
        //     keyName: 'unitId',
        //     viewName: 'unitName',
        //     type: SelectCompanyAndProject,
        //     label: '成果所有者',
        //     onSelectChange: (e) => {
        //       this.form.unitName = e?.name
        //       this.form.unitCode = e?.code
        //     }
        //   },
      
         
          {
            keyName: 'resultLevel',
            label: '举办单位',
            // placeholder:'仅编辑时可用',
            // disabled: this.editModel !== 1,
            type: 'a-select',
            data: [
              {
                label: '优秀',
                value: 0
              },
              {
                label: '一般',
                value: 1
              },
              {
                label: '差',
                value: 2
              }
            ],
            customRender(e) {
              return ['优秀', '一般', '差'][e] || '无'
            }
          },
          {
            keyName: 'remark',
            type: 'BaseInput',
            label: '奖项级别',
            disabled: this.editModel !== 1,
          },
          {
            keyName: 'remark',
            type: 'BaseInput',
            label: '证书原件保管单位'
          }
        ]
      }
    },
    methods: {
      /**
       * 提交表单
       */
      handleSubmit() {
        let that = this
        let url = [updateBimResult, insertBimResult][that.editModel - 1] // 根据操作切换url地址
        // 校验表单
        that.$refs.ruleForm.validate((e) => {
          if (e) {
            // 提交表单
            that.loading = true
            let form = that.$clone(that.form)
            url(form)
              .then((res) => {
                that.loading = false
                if (res.code === '0' || res.data.code === '0') {
                  that.$message.success('提交成功！')
                  that.$multiTab.close()
                }
              })
              .catch((res) => {
                that.loading = false
              })
          }
        })
      }
    }
  }
  </script>